layui.define(['table', 'admin', 'form', 'ax', 'upload'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var upload = layui.upload;
    /**
     * 门店装修表管理
     */
    var Mdzx = {
        tableId: "mdzx_ys_Table"
    };

    /**
     * 初始化表格的列
     */
    Mdzx.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: true},
           {type: 'numbers', title: '序号', align: "center", hide: true, width: 40, fixed: true},
            {field: 'btyZxMdzxId', hide: true, title: '主键'},
            // {field: 'btyBrandId', sort: true, title: '门店id'},
            {field: 'btyBrandIdView', align: "left", title: '门店名称', width: 190, fixed: true},
            // {field: 'mdBianma', align: "center", title: '妖码'},
            {field: 'mdBianma', align: "center", title: '妖码', templet: function (d) {
                    if (d.mdBianma === 'undefined') {
                        return "";
                    } else {
                        return d.mdBianma;
                    }
                }},

            {field: 'mianji', title: '总面积(m²)', minWidth: 88, align: "center",},
            {
                field: 'yusuanHeji',
                title: '预算合计',
                totalRow: true,
                align: "center",
                minWidth: 88,
                templet: function (row) {
                    if (row.yusuanHeji == null || row.yusuanHeji == '') {
                        return "0";
                    } else {
                        return row.yusuanHeji;
                    }
                }
            },
            {field: 'yujiTouzi', title: '预计投资',templet: '#yujiTouziTpl', totalRow: true, align: "center", minWidth: 88},
            // {
            //     field: 'chengbenHeji',
            //     title: '成本总计C',
            //     totalRow: true,
            //     align: "center",
            //     width: 100,
            //     templet: function (row) {
            //         if (row.chengbenHeji == null || row.chengbenHeji == '') {
            //             return "0";
            //         } else {
            //             return row.chengbenHeji;
            //         }
            //     }
            // },
            // {
            //     field: 'touzishengyu',
            //     title: '投资剩余T=Y-C',
            //     totalRow: true,
            //     align: "center",
            //     width: 130,
            //     templet: function (row) {
            //         if (row.touzishengyu == null || row.touzishengyu == '') {
            //             return "0";
            //         } else {
            //             return row.touzishengyu;
            //         }
            //     }
            // },
            {align: 'center', toolbar: '#mdzx_tableBar', title: '操作', minWidth: 140}
        ]];
    };

    /**
     * 监听复选框选择
     */
    // table.on('checkbox(mdzxTable)', function(obj){
    //     window.location.href = Feng.ctxPath + '/zxChengben';
    // });


    /**
     * 点击查询按钮
     */
    Mdzx.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['status'] = $("#status").val();
        queryData['yusuanLock'] = $("#yuSuanLockMdzx").val();
        table.reload(Mdzx.tableId, {where: queryData});
    };

    $('#condition').change(function () {
        Mdzx.search();
    });

    /**
     * 弹出添加对话框
     */
    Mdzx.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/mdzx/add';
    };

    /**
     * 导出excel按钮
     */
    // Mdzx.exportExcel = function () {
    //     var checkRows = table.checkStatus(Mdzx.tableId);
    //     if (checkRows.data.length === 0) {
    //         Feng.error("请选择要导出的数据");
    //     } else {
    //         table.exportFile(tableResult.config.id, checkRows.data, 'xls');
    //     }
    // };

    Mdzx.exportExcel = function () {
        var checkRows = table.checkStatus(Mdzx.tableId);
        var data = checkRows.data;
        var ids = "";
        //选择了门店才可以导出
        // if (checkRows.data.length === 0) {
        //     Feng.error("请先选择装修门店");
        // }
        //var btyZxMdzxIdDaoChu = $("#btyZxMdzxIdDaoChu").val();
        // alert(btyZxMdzxIdDaoChu);
        //     if (btyZxMdzxIdDaoChu == "" || btyZxMdzxIdDaoChu == undefined || btyZxMdzxIdDaoChu == null) {
        //         Feng.error("请先选择装修门店");
        //     }
        // else {
            for (var i in data) {
                ids += data[i].btyZxMdzxId + ",";
            }
            if (ids === "") {
                Feng.error("请先选择装修门店");
                return;
            }
            // alert(ids);
            // return false;
            //获取导出Excel的数据
            var url = Feng.ctxPath + "/zxYusuan/listExports?ids=" + ids;
            location.href = url;
       // }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Mdzx.openEditDlg = function (data) {
        parent.layer.open({
            type: 2,
            title: '修改门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/mdzx/edit?btyZxMdzxId=' + data.btyZxMdzxId,
            end: function () {
                Mdzx.search();
                layui.shuxinYusuan(data.btyZxMdzxId, data.btyBrandIdView);
            },
            cancel:function (index){
                Mdzx.search();
                layui.shuxinYusuan(data.btyZxMdzxId, data.btyBrandIdView);
            }
        });
        // window.location.href = Feng.ctxPath + '/mdzx/edit?btyZxMdzxId=' + data.btyZxMdzxId;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Mdzx.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/mdzx/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                    table.reload(Mdzx.tableId);
                } else {
                    Feng.error(data.message);
                }
            });
            ajax.set("btyZxMdzxId", data.btyZxMdzxId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Mdzx.tableId,
        url: Feng.ctxPath + '/mdzx/list',
        page: true,
        even: true,
        cellMinWidth: 80,
        totalRow: true,
        cols: Mdzx.initColumn()
    });

    // 搜索按钮点击事件
    $('#mdzx_btnSearch').click(function () {
        Mdzx.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Mdzx.openAddDlg();
    });

    //点击复制按钮
    $('#btnCopy').click(function () {

        var btyZxMdzxId = $("#btyZxMdzxId").val();
        if (btyZxMdzxId=="") {
            Feng.error("请选择一个门店");
            return false;
        }

        parent.layer.open({
            type: 2,
            title: '复制门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/mdzx/copy?btyZxMdzxId=' + btyZxMdzxId,
            end: function () {
                 Mdzx.search();
                // Feng.success("复制成功！");
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    });
    Mdzx.batchLock = function (yuSuanLock) {
        var checkStatus = table.checkStatus(Mdzx.tableId);
        var dataSelect = checkStatus.data;
        if(dataSelect.length==0){
            Feng.error("请选择门店");
            return false;
        }

        var ids = null;
        for(var i=0;i<dataSelect.length;i++){
            var d = dataSelect[i];
            var id = d.btyZxMdzxId;
            if(ids==null){
                ids = id;
            }else {
                ids+=","+id;
            }
        }

        var ajax = new $ax(Feng.ctxPath + "/mdzx/batchLock", function (data) {
            if (data.code == 200) {
                if(yuSuanLock==1){
                    Feng.success("锁定成功");
                }else if(yuSuanLock==0) {
                    Feng.success("解锁成功");
                }
                Mdzx.search();
            } else {
                Feng.error(data.message);
            }
        }, function (data) {
            Feng.error("失败!" + data.responseJSON.message + "!");
        });
        ajax.set("yusuanLockPass","123456");
        ajax.set("yusuanLock", yuSuanLock);
        ajax.set("ids",ids);
        ajax.start();
    };
    //批量锁定
    $('#bt_suoding').click(function () {
        var operation = function () {
            Mdzx.batchLock(1);
        };
        Feng.confirm("是否批量锁定?", operation);

    });


    //批量解锁
    $('#bt_jiesuo').click(function () {
        var operation = function () {
            Mdzx.batchLock(0);
        };
        Feng.confirm("是否批量解锁?", operation);
    });



    // 导出excel
    // $('#btnExp').click(function () {
    //     Mdzx.exportExcel();
    // });

    // 工具条点击事件
    table.on('tool(' + Mdzx.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'mdzx_edit') {
            Mdzx.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Mdzx.onDeleteItem(data);
        } else if (layEvent === 'investmentSituation') {
            Mdzx.costSetting(data);
        }else if(layEvent==='viewYuanSuan'){
            Mdzx.viewYuanSuan(data);
        }
    });

    /**
     * 查看预算
     */
    Mdzx.viewYuanSuan = function (data) {
        window.location.href = Feng.ctxPath + '/zxYusuan/viewYuanSuan?btyZxMdzxId=' + data.btyZxMdzxId;
    };

    /**
     * 投资情况显示
     */
    Mdzx.costSetting = function (data) {
        window.location.href = Feng.ctxPath + '/mdzx/tzqk?btyZxMdzxId=' + data.btyZxMdzxId;
    };

    // /**
    //  * 初始化门店
    //  */
    // var languageAjax = new $ax(Feng.ctxPath + "/mdzx/list");
    // var languageResult = languageAjax.start();
    // console.info(languageResult);
    // $.each(languageResult.data, function (index, val) {
    //     var pinyin = ConvertPinyin(val.btyBrandIdView);
    //     //查询需要选择的门店
    //     $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "</option>");
    // });
    // form.render('select');

    // 导出excel
    $('#btnExp').click(function () {
        Mdzx.exportExcel();
    });

    __log = function(msg) {
        setTimeout(() => {
            throw new Error(msg);
        }, 0);
    }

    //监听行单击事件（双击事件为：rowDouble）
    table.on('row(mdzx_ys_Table)', function(obj){
        var data = obj.data;

        // leez modify
        $('div[lay-id="mdzx_ys_Table"]').find(".layui-table-body tr ").attr({ "style": "" });//其他tr恢复原样

        $('div[lay-id="mdzx_ys_Table"]').find(obj.tr.selector).attr({ "style": "background:#10b1c6!important; color: #FFF!important" });//改变当前tr颜色

        let index = $('div[lay-id="mdzx_ys_Table"] tr').index($('div[lay-id="mdzx_ys_Table"]').find(obj.tr.selector))
        $('#leftRowSelected').val(index)
        // leez modify

        var btyZxMdzxId = data.btyZxMdzxId;
        var btyBrandIdView=data.btyBrandIdView;
        layui.shuxinYusuan(btyZxMdzxId,btyBrandIdView);
    });

    exports('shuxinMd', function () {//函数参数
        Mdzx.search();
        // leez add 右侧修改刷新左侧时 左侧能高亮之前修改的行目
        setTimeout(() => {
            let lastIndex = $('#leftRowSelected').val()
            let _tr = $('div[lay-id="mdzx_ys_Table"] tr:eq('+ lastIndex +')' )
            let _tr2 = $('div[lay-id="mdzx_ys_Table"] .layui-table-fixed tr:eq('+ lastIndex +')' )
            _tr.css('background','#10b1c6')
            _tr2.css('background','#10b1c6')
        }, 100)
        // leez add
    });
});
